/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.pay.db.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.base.model.ClassCode;
import com.yunhoo.pay.model.Expense;
import com.yunhoo.pay.util.BillNumBuilder;
import com.yunhoo.pay.util.BillOrder;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.Null;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class CostProofImpl {

    private static String nameSpace = "costProof.";

    private static Map paramMap() {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        return map;
    }

    public static void insertCostProof(Expense exp) {
        Map map = paramMap();
        map.put("voutype", exp.getVoutype());
        map.put("numDate", exp.getNumDate());
        map.put("number", exp.getNumber());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("taxAmount", Null.convert(exp.getTaxAmount()));
        map.put("remain", exp.getRemain());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("affixAmount", exp.getAffixAmount());
        map.put("person", exp.getUser().getCpsn_number());
        map.put("deptcode", exp.getDept().getCode());
        map.put("payStyle", exp.getPayStyle().getCode().trim());
        map.put("memo", exp.getMemo());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        IbatisTemplate.insert(nameSpace + "insertCostProof", map);
    }

    public static String getCostProofNumber(SqlMapClient client) throws SQLException {
        String ExpenseMaxNumber = (String) client.queryForObject("costProof.maxCostProofNumber", LoginParam.getOrg().getCode());
        if (ExpenseMaxNumber == null || ExpenseMaxNumber.isEmpty()) {
            return "000001";
        }
        ExpenseMaxNumber = String.format("%06d%n", Integer.parseInt(ExpenseMaxNumber.trim()) + 1);
        return ExpenseMaxNumber.trim();
    }

    public static void insert(Expense exp) {
        SqlMapClient client = IbatisTemplate.getSqlMapClient();
        try {
            client.startTransaction();

            String number = BillNumBuilder.buildNum(client);
            exp.setNumber(number);

            Map map1 = new HashMap();
            map1.put("number", exp.getNumber());
            map1.put("item", exp.getFeeItem().getCode().trim());
            map1.put("amount", exp.getTotal());
            map1.put("orgCode", LoginParam.getOrg().getCode());
            client.insert(nameSpace + "insertFeeDetail", map1);

            Map map2 = paramMap();
            map2.put("voutype", exp.getVoutype());
            map2.put("numDate", exp.getNumDate());
            map2.put("number", exp.getNumber());
            map2.put("projCode", Null.convert(exp.getProject().getFinCode()));
            map2.put("affair", exp.getAffair());
            map2.put("total", exp.getTotal());
            map2.put("taxAmount", Null.convert(exp.getTaxAmount()));
            map2.put("remain", exp.getRemain());
            map2.put("numberStatus", exp.getNumberStatus());
            map2.put("affixAmount", exp.getAffixAmount());
            map2.put("person", exp.getUser().getCpsn_number());
            map2.put("deptcode", exp.getDept().getCode());
            map2.put("payStyle", exp.getPayStyle().getCode().trim());
            map2.put("memo", exp.getMemo());
            map2.put("payDept", exp.getPayDept().getCode());
            map2.put("feePerson", exp.getFeePer().getCpsn_number().trim());
            client.insert(nameSpace + "insertCostProof", map2);

            client.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(CostProofImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                client.endTransaction();
            } catch (SQLException ex) {
                Logger.getLogger(CostProofImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void insertFeeDetail(Map c) {
        IbatisTemplate.insert(nameSpace + "insertFeeDetail", c);
    }

    public static List<ClassCode> feeItems() {
        return IbatisTemplate.queryForList(nameSpace + "feeItems", null);
    }

    public static List proofListByPerson(Map map) {
        return IbatisTemplate.queryForList(nameSpace + "proofListByPerson", map);
    }

    public static List proofListForCheck() {
        return IbatisTemplate.queryForList(nameSpace + "proofListForCheck", null);
    }

    public static List proofListForPay() {
        return IbatisTemplate.queryForList(nameSpace + "proofListForPay", null);
    }

    public static void update(Expense exp) {
        SqlMapClient client = IbatisTemplate.getSqlMapClient();
        try {
            client.startTransaction();

            Map map1 = paramMap();
            map1.put("number", exp.getNumber());
            map1.put("numDate", exp.getNumDate());
            map1.put("projCode", Null.convert(exp.getProject().getFinCode()));
            map1.put("affair", exp.getAffair());
            map1.put("total", exp.getTotal());
            map1.put("taxAmount", Null.convert(exp.getTaxAmount()));
            map1.put("remain", exp.getRemain());
            map1.put("payStyle", exp.getPayStyle().getCode().trim());
            map1.put("memo", exp.getMemo());
            map1.put("numberStatus", exp.getNumberStatus());
            map1.put("affixAmount", exp.getAffixAmount());
            map1.put("payDept", exp.getPayDept().getCode());
            map1.put("feePerson", exp.getFeePer().getCpsn_number().trim());
            client.update(nameSpace + "updateCostProofByPerson", map1);

            Map map2 = new HashMap();
            map2.put("number", exp.getNumber());
            map2.put("feeItem", exp.getFeeItem().getCode().trim());
            map2.put("amount", exp.getTotal());
            map2.put("orgCode", LoginParam.getOrg().getCode());
            client.update(nameSpace + "updateFeeDetail", map2);

            client.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(CostProofImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                client.endTransaction();
            } catch (SQLException ex) {
                Logger.getLogger(CostProofImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void updateCostProofByPerson(Expense exp) {
        Map map = paramMap();
        map.put("number", exp.getNumber());
        map.put("numDate", exp.getNumDate());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("taxAmount", Null.convert(exp.getTaxAmount()));
        map.put("remain", exp.getRemain());
        map.put("payStyle", exp.getPayStyle().getCode().trim());
        map.put("memo", exp.getMemo());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("affixAmount", exp.getAffixAmount());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        IbatisTemplate.update(nameSpace + "updateCostProofByPerson", map);
    }

    public static void updateFeeDetail(Map map) {
        IbatisTemplate.update(nameSpace + "updateFeeDetail", map);
    }

    public static void updateProofForCheck(Expense exp) {
        Map map = paramMap();
        map.put("voutype", exp.getVoutype().trim());
        map.put("number", exp.getNumber());
        map.put("numstatus", exp.getNumberStatus());
        map.put("checkaccount", exp.getCheckaccount());
        map.put("remain", exp.getRemain());
        map.put("budgetsource", exp.getBudgetSource());
        map.put("eoc", exp.getCostitem().getCode());
        map.put("paycode", exp.getPaycode());
        map.put("costclass", exp.getCostClass());
        map.put("costtype", exp.getCostTypes().getCode());
        map.put("auditor", exp.getAuditor());
        map.put("auditdate", exp.getAuditDate());
        IbatisTemplate.update("commOper.updateVoucherForCheck", map);
    }

    public static void submitProof(Map map) {
        IbatisTemplate.update("commOper.submitVoucher", map);
    }

    public static void rescindVoucher(Map map) {
        IbatisTemplate.update("commOper.rescindVoucher", map);
    }

    public static void delProofByNumber(String number) {
        Map map = paramMap();
        map.put("number", number);
        IbatisTemplate.delete(nameSpace + "delProofByNumber", number);
    }

    public static void delProofFeeDetailByNumber(String number) {
        Map map = paramMap();
        map.put("number", number);
        IbatisTemplate.delete(nameSpace + "delProofFeeDetailByNumber", number);
    }

    public static List proofResultList(Map map) {
        map.put("billOrder", BillOrder.checkOrder);//单据排列顺序
        return IbatisTemplate.queryForList(nameSpace + "proofResultList", map);
    }
    

    public static List proofListPay(Map map) {
        map.put("billOrder", BillOrder.payOrder);//单据排列顺序
        return IbatisTemplate.queryForList(nameSpace + "proofResultList", map);
    }

    public static Expense proofByNumber(String number) {
        return (Expense) IbatisTemplate.query(nameSpace + "proofByNumber", number);
    }

    public static List getVoucherList(Map map) {
        map.put("billOrder", BillOrder.checkOrder);//单据排列顺序
        return IbatisTemplate.queryForList(nameSpace + "getVoucherList", map);
    }
    
    public static List getVoucherListPay(Map map) {
        map.put("billOrder", BillOrder.payOrder);//单据排列顺序
        return IbatisTemplate.queryForList(nameSpace + "getVoucherList", map);
    }
}
